package com.drought.rained.ui.activity.login;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.drought.rained.R;
import com.drought.rained.LJApplication;
import com.drought.rained.base.BaseTitleActivity;
import com.drought.rained.business.RouterMapLJ;
import com.drought.rained.cache.AcacheLJ;
import com.drought.rained.cache.AcacheKeysLJ;
import com.drought.rained.common.ConstantLJ;
import com.drought.rained.common.RouterCommandLJ;
import com.drought.rained.mvpview.LoginMvpViewLJ;
import com.drought.rained.presenter.LoginPresenterLJ;
import com.drought.rained.response.LoginMethodResponseLJ;
import com.drought.rained.response.LoginResponseLJ;
import com.drought.rained.rxbus.EventMsgLJ;
import com.drought.rained.rxbus.EventsLJ;
import com.drought.rained.rxbus.RxBusLJ;
import com.drought.rained.utils.StringUtilsLJ;
import com.drought.rained.utils.ToastUtilsLJ;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import crossoverone.statuslib.StatusUtil;

@SuppressLint("CheckResult")
@Route(name = "登录页", path = RouterMapLJ.LOGIN_PAGE)
public class LoginActivityLJ extends BaseTitleActivity implements TextWatcher, LoginMvpViewLJ {

    @BindView(R.id.user_name_text)
    EditText editUserName;
    @BindView(R.id.password_text)
    EditText editPassWord;
    @BindView(R.id.show_psw_text)
    CheckBox showPswText;
    @BindView(R.id.btn_login)
    Button btnLogin;
    @BindView(R.id.btn_register)
    TextView btnRegister;
    @BindView(R.id.btn_forget_psw)
    TextView btnForgetPsw;

    private LoginPresenterLJ loginPresenter;

    @Override
    protected void initViews() {
        ButterKnife.bind(this);
        editPassWord.setTransformationMethod(PasswordTransformationMethod.getInstance());
        showPswText.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    editPassWord.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                } else {
                    editPassWord.setTransformationMethod(PasswordTransformationMethod.getInstance());
                }
            }
        });
        editUserName.addTextChangedListener(this);
        editPassWord.addTextChangedListener(this);
        btnLogin.setEnabled(false);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        if (intent != null && !StringUtilsLJ.isEmpty(intent.getStringExtra(RouterCommandLJ.USER_NAME))) {
            editUserName.setText(intent.getStringExtra(RouterCommandLJ.USER_NAME));
        }
        loginPresenter = new LoginPresenterLJ(this);
    }

    @Override
    protected void onUIReady() {
        if (getIntent() != null && !StringUtilsLJ.isEmpty(getIntent().getStringExtra(RouterCommandLJ.USER_NAME))) {
            editUserName.setText(getIntent().getStringExtra(RouterCommandLJ.USER_NAME));
        }
        loginPresenter = new LoginPresenterLJ(this);
    }

    @Override
    protected void setSystemInvadeBlack() {
        StatusUtil.setUseStatusBarColor(this, getResources().getColor(R.color.colorTransparent));
        // 第二个参数是是否沉浸,第三个参数是状态栏字体是否为黑色。
        StatusUtil.setSystemStatus(this, true, false);
    }

    @Override
    protected int getContentResId() {
        return R.layout.layout_of_login;
    }

    @Override
    protected int getResId() {
        return 0;
    }

    @OnClick({R.id.btn_login, R.id.btn_register, R.id.btn_forget_psw})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_login: // 登陆
                String hintStr = checkData();
                if (StringUtilsLJ.isEmpty(hintStr)) {
                    loginPresenter.requestLogin(editUserName.getText().toString().trim(),
                            editPassWord.getText().toString().trim());
                    showLoadingView();
                } else {
                    ToastUtilsLJ.showShort(this, hintStr);
                }
                break;
            case R.id.btn_register: // 注册
                ARouter.getInstance().build(RouterMapLJ.REGISTER_PAGE).navigation();
                break;
            case R.id.btn_forget_psw: // 忘记密码
                ARouter.getInstance().build(RouterMapLJ.FIND_PASSWORD_PAGE).navigation();
                break;
        }
    }

    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
    }

    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        if (!StringUtilsLJ.isEmpty(editUserName.getText().toString())
                && !StringUtilsLJ.isEmpty(editPassWord.getText().toString())) {
            btnLogin.setEnabled(true);
        } else if (btnLogin.isEnabled()) {
            btnLogin.setEnabled(false);
        }
    }

    @Override
    public void afterTextChanged(Editable editable) {
    }

    // 检查数据
    private String checkData() {
        if (editUserName != null && editPassWord != null) {
            String uerName = editUserName.getText().toString();
            String passwordStr = editPassWord.getText().toString();
            if (StringUtilsLJ.isEmpty(uerName)) {
                return getStringResource(R.string.login_type_user_name);
            }
            if (StringUtilsLJ.isEmpty(passwordStr)) {
                return getStringResource(R.string.login_type_pwd);
            }
        }
        return null;
    }

    @Override
    public void onGetDataNext(LoginResponseLJ response) {
        dismissLoadingView();
        if (response.getStatus() == ConstantLJ.HTTP_OK) {
            AcacheLJ.get(LJApplication.getContext()).put(AcacheKeysLJ.TOKEN, response.getData().getToken());
            EventMsgLJ msg = new EventMsgLJ();
            msg.setMsg(EventsLJ.LOGIN_SUCCESS);
            RxBusLJ.getInstance().post(msg);
            finish();
        } else {

            ToastUtilsLJ.showShort(LoginActivityLJ.this, response.getMessage());
        }
    }


    @Override
    public void onGetDataNext(LoginMethodResponseLJ response) {

    }

    @Override
    public void onGetDataError(Throwable throwable) {
        dismissLoadingView();
    }
}
